package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.utils.MyConnection;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class UserDaoImpl implements UserDao {

    //注入queryRunner
    @Autowired
    private QueryRunner queryRunner;

    //注入myConnnection
    @Autowired
    private MyConnection myConnection;

    @Override
    public void out(String outName, int money) throws SQLException {

        String sql = "update tal_account set money = money - ? where username = ?";

        Object[] params = {
                money, outName
        };

        int count = queryRunner.update(myConnection.getConnection(), sql, params);

        System.out.println(count);

    }

    @Override
    public void in(String inName, int money) throws SQLException {

        String sql = "update tal_account set money = money + ? where username = ?";
        Object[] params = {
                money, inName
        };

        int count = queryRunner.update(myConnection.getConnection(), sql, params);

        System.out.println(count);

    }
}
